/**
 * Created by 小暖 on 2017/8/8.
 */


function Map() {//初始化map_,给map_对象增加方法，使map_像Map
    var map_ = {};
    map_.put = function (key, value) {
        map_[key + '_'] = value;
    };
    map_.get = function (key) {
        return map_[key + '_'];
    };
    map_.remove = function (key) {
        delete map_[key + '_'];
    };
    map_.keyset = function () {
        var ret = "";
        for (var p in map_) {
            if (typeof p == 'string' && p.substring(p.length - 1) == "_") {
                ret += ",";
                ret += p.substring(0, p.length - 1);
            }
        }
        if (ret == "") {
            return ret.split(",");
        } else {
            return ret.substring(1).split(",");
        }
    };
    return map_;
}
function todate(timestamp) {
    if (timestamp !=null ) {
        return new Date(timestamp.time).toLocaleString().replace("/", "-").replace("/", "-").replace(/下午|上午/g, "");
    }else{
        return "";
    }
}
function curDateTime() {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var date = d.getDate();
    var curDateTime = year;
    if (month > 9)
        curDateTime = curDateTime + "-" + month;
    else
        curDateTime = curDateTime + "-0" + month;
    if (date > 9)
        curDateTime = curDateTime + "-" + date;
    else
        curDateTime = curDateTime + "-0" + date;
    return curDateTime;
}

provinces = [
    {
        "dm": "110000",
        "mc": "北京市"
    },
    {
        "dm": "120000",
        "mc": "天津市"
    },
    {
        "dm": "130000",
        "mc": "河北省"
    },
    {
        "dm": "140000",
        "mc": "山西省"
    },
    {
        "dm": "150000",
        "mc": "内蒙古自治区"
    },
    {
        "dm": "210000",
        "mc": "辽宁省"

    },
    {
        "dm": "220000",
        "mc": "吉林省"
    },
    {
        "dm": "230000",
        "mc": "黑龙江省"
    },
    {
        "dm": "310000",
        "mc": "上海市"

    },
    {
        "dm": "320000",
        "mc": "江苏省"
    },
    {
        "dm": "330000",
        "mc": "浙江省"
    },
    {
        "dm": "340000",
        "mc": "安徽省"
    },
    {
        "dm": "350000",
        "mc": "福建省"
    },
    {
        "dm": "360000",
        "mc": "江西省"
    },
    {
        "dm": "370000",
        "mc": "山东省"
    },
    {
        "dm": "410000",
        "mc": "河南省"
    },
    {
        "dm": "420000",
        "mc": "湖北省"
    },
    {
        "dm": "430000",
        "mc": "湖南省"
    },
    {
        "dm": "440000",
        "mc": "广东省"
    },
    {
        "dm": "450000",
        "mc": "广西壮族自治区"
    },
    {
        "dm": "460000",
        "mc": "海南省"
    },
    {
        "dm": "500000",
        "mc": "重庆市"
    },
    {
        "dm": "510000",
        "mc": "四川省"
    },
    {
        "dm": "520000",
        "mc": "贵州省"
    },
    {
        "dm": "530000",
        "mc": "云南省"
    },
    {
        "dm": "540000",
        "mc": "西藏自治区"
    },
    {
        "dm": "610000",
        "mc": "陕西省"
    },
    {
        "dm": "620000",
        "mc": "甘肃省"
    },
    {
        "dm": "630000",
        "mc": "青海省"
    },
    {
        "dm": "640000",
        "mc": "宁夏回族自治区"
    },
    {
        "dm": "650000",
        "mc": "新疆维吾尔自治区"
    },
    {
        "dm": "710000",
        "mc": "台湾省"
    },
    {
        "dm": "810000",
        "mc": "香港特别行政区"
    },
    {
        "dm": "820000",
        "mc": "澳门特别行政区"
    }
];


function loadRegions() {
    $("input[name='location']").remove();
    $("#region").empty();
    var obj = "<option value=''>选择地区</option>";
    for (var i in provinces) {
        obj += "<option value='" + provinces[i].dm + "'>" + provinces[i].mc + "</option>";
    }
    $("#region").append("<select id='province' name='areaCode' class='form-control col-md-1' style='width:130px;float: left;'>" + obj + "</select>");
    $("#province").closest("div").prepend("<input id='cityCode' name='cityCode' style='display: none;'></input>");
    $("#province").change(function () {
        var data = $(this).val();
        $("#province").nextAll().remove();
        if (!data) return;
        if(data =='810000' || data =='820000' ){
            $("#province").nextAll().remove();
            $("#province").attr("name","areaCode");
            var provinceName = getSelectName("province");
            $("#cityCode").val(provinceName);
            return;
        }
        $.post('/admin/city.action', {"province": data}, function (result) {
            //result = JSON.parse(result);
            $("#province").removeAttr("name");
            var cities = result.cities;
            var obj = "<option value=''>选择地区</option>";
            for (var j in cities) {
                obj += "<option value='" + cities[j].dm + "'>" + cities[j].mc + "</option>";
            }
            $("#province").closest("div").append("<select id='select_2' name='areaCode' class='form-control col-md-1' style='width:130px;float: left;'>" + obj + "</select>");
            var province = getSelectName("province");

            $("#select_2").change(function () {
                var data = $(this).val();
                if(data =='442000' || data =='441900' ){
                    $("#select_2").nextAll().remove();
                    $("#select_2").attr("name","areaCode");
                    var provinceName = getSelectName("province");
                    var cityName = getSelectName("select_2");
                    $("#cityCode").val(provinceName+cityName);
                    return;
                }
                $("#province").closest("div").find("select:eq(2)").remove();
                $.post('/admin/region.action', {"city": data}, function (result) {
                    //result = JSON.parse(result);
                    var regions = result.regions;
                    if (regions != null) {
                        $("#select_2").removeAttr("name");
                        var obj = "";
                        for (var k in regions) {
                            obj += "<option value='" + regions[k].dm + "'>" + regions[k].mc + "</option>";
                        }
                        $("#province").closest("div").append("<select id='select_3' name='areaCode' class='form-control col-md-1' style='width:130px'>" + obj + "</select>");
                        $("#cityCode").val(regions[0].described);
                        $("#select_3").change(function () {
                            var provinceName = getSelectName("province");
                            var cityName = getSelectName("select_2");
                            var regionName =  getSelectName("select_3");
                            $("#cityCode").val(provinceName+cityName+regionName);
                        });
                    }else{
                        $("#select_2").attr("name","areaCode");
                        var provinceName = getSelectName("province");
                        var cityName = getSelectName("select_2");
                        $("#cityCode").val(provinceName+cityName);
                    }
                },'json')
            });
        },'json')
    });
}

function getSelectName(idName){
    var city = document.getElementById(idName);
    var index = city.selectedIndex;
    var cityName = city.options[index].text;
    return cityName;
}

function loadCity() {
    $("input[name='location']").remove();
    $("#region").empty();
    var obj = "<option value=''>选择地区</option>";
    for (var i in provinces) {
        obj += "<option value='" + provinces[i].dm + "'>" + provinces[i].mc + "</option>";
    }
    $("#region").append("<select id='province' name='location' class='form-control col-md-1' style='width:130px'>" + obj + "</select>");
    $("#province").select2({
        "placeholder": "选择地区",
        "width": "130px"
    })
    $("#province").change(function () {
        var data = $(this).val();
        $("#province").attr("name","location");
        $("#region").attr("style","width: 16%;padding-left: 0px;");
        $("#province").nextAll().remove();
        $("#province").attr("class","form-control col-md-1");
        $("#province").select2({
            "width": "130px"
        })

        $("#province").closest("div").append("<input id='cityCode' name='cityCode' style='display: none;'></input>");
        var provinceName = getSelectName("province");
        $("#cityCode").val(provinceName);
        if (!data) return;
        if(data !='120000' && data !='110000' && data !='310000' && data !='500000' && data !='710000'&& data !='810000'&& data !='820000'){
            $.post('/admin/city.action', {"province": data}, function (result) {
                result = JSON.parse(result);
                var obj = "<option value='0'>选择地区</option>";
                $("#province").removeAttr("name");
                var cities = result.cities;
                for (var i in cities) {
                    obj += "<option value='" + cities[i].dm + "'>" + cities[i].mc + "</option>";
                }
                $("#region").attr("style","width: 26%;padding-left: 0px;");
                $("#province").closest("div").append("<select id='select_2' name='location' class='form-control col-md-1' style='width:130px'>" + obj + "</select>");
                $("#select_2").select2({
                    "placeholder": "选择地区",
                    "width": "130px"
                })

                $("#select_2").change(function () {
                    var provinceName = getSelectName("province");
                    var cityName = getSelectName("select_2");
                    $("#cityCode").val(provinceName+cityName);
                });
            })
        }
    });
}